package com.nextbus.main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MoreInfo extends ListActivity {
    private static final String TAG = "MoreInfo";

    public static final String ALL_ROUTES = "ALL Routes";

    public static final String PREDICTIONS_KEY = "predictions";

    public static final String ROUTE_KEY = "route";

    public static final String ROUTE_TEXT_KEY = "routeText";

    public static final String ROUTE_TITLES_KEY = "routeTitles";

    public static final String STOPS_KEy = "stops";

    public static final String TEXT_KEY = "text";

    public static final String TITLE_KEY = "title";

    private static final String[] routes = { "All Routes", "555" };

    private Spinner sp;

    private TextView stop_title_text;

    private TextView stopId_route_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.moreinfo);
        setupViews();
    }

    /**
     * init the class
     */
    public void setupViews() {

        /**
         * bundle 里面存储大量信息， 比如这个界面是由Stop的Balloon中的more info触发而来，应该本身包含了
         * 本站点的相应信息，通过startActivity()到达MoreInfo，Bundel存储 route，predictions等信息
         * ps，prediction提供了信息转换处理功能 将动态信息转换Str，即spinnerAdapter中new
         * String[]{"text"}对应
         */
        sp = (Spinner) findViewById(R.id.moreinfo_route_spinner);
        stop_title_text = (TextView) findViewById(R.id.moreinfo_title1);
        stopId_route_text = (TextView) findViewById(R.id.moreinfo_title2);

        stop_title_text.setText("Specified Stop Title");
        stopId_route_text.setText("StopId and route");

        // init spinner
        // set spinner item listener
        refreshSpinnerAdapter();
        // init listview
        refreshListViewSimpleAdapter(null);
    }

    public void refreshSpinnerAdapter() {
        Log.d(TAG, "first init spinner adapter");
        ArrayAdapter adapter = new ArrayAdapter(MoreInfo.this, android.R.layout.simple_spinner_item, routes);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp.setAdapter(adapter);
        sp.setOnItemSelectedListener(new MoreInfo1());
    }

    public void refreshListViewSimpleAdapter(String route) {
        /**
         * 当route为ALL_ROUTES,及非ALL_ROUTES，lists要及时更新
         */
        if (route == null) {
            Log.d(TAG, "first init listiview");
            List lists = new ArrayList();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("text", "hello");
            lists.add(map);
            map = new HashMap<String, Object>();
            map.put("text", "world");
            lists.add(map);
            SimpleAdapter simpleAdapter = new SimpleAdapter(MoreInfo.this,
                    (List<Map<String, Object>>) lists, R.layout.moreinfo_row,
                    new String[] { "text" },
                    new int[] { R.id.moreinfo_row_text });
            simpleAdapter.setViewBinder(new MoreInfo2());
            setListAdapter(simpleAdapter);
        }
    }
}
